package zsw.Serializable;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XMLSerializable2 {
	
	public static void main(String[] args) {
		Person person=new Person();
		person.setAge(12);
		person.setName("zws");
		person.setSex("男");
		User s=new User();
		s.setFirstName("ds");
		s.setId("224");
		person.setUser(s);
		XMLSerializable2 serializable2=new XMLSerializable2();
		String serialize = serializable2.serialize(person);
		System.out.println(serialize);

		
		/*Person deserialize = serializable2.deserialize(serialize, Person.class);
		System.out.println(deserialize);*/
		
	}
	
	
	public String serialize(Object  obj) {
		
		XStream xStream=new XStream(new DomDriver());
		 xStream.alias(obj.getClass().getSimpleName(), obj.getClass());
		String xml = xStream.toXML(obj);
		return xml;
	}
	
	
	public <T> T deserialize(String xmlStr,Class<T> clazz) {
		XStream xStream=new XStream(new DomDriver());
		xStream.alias(clazz.getSimpleName(), clazz);
		return (T) xStream.fromXML(xmlStr);
		
	}
	

}


